package com.hemerasolutions.v2trainer.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.stereotype.Repository;

import com.hemerasolutions.v2trainer.dao.interfaces.VersionableEquipmentDaoInterface;
import com.hemerasolutions.v2trainer.model.equipment.VersionableEquipment;
import com.hemerasolutions.v2trainer.model.equipment.VersionableEquipment_;
import com.hemerasolutions.v2trainer.model.exercise.Equipment;
import com.hemerasolutions.v2trainer.model.exercise.Equipment_;
import com.hemerasolutions.v2trainer.model.library.Library;

@Repository(value = "vEquipmentDao")
public class VersionableEquipmentDao extends BaseDao<VersionableEquipment> implements VersionableEquipmentDaoInterface{
	private static final long serialVersionUID = 1L;

	@SuppressWarnings("unchecked")
	@Override
	public List<VersionableEquipment> searchVersionableEquipments(Map<String, Object> fields) {
		List<Predicate> predicateList = new ArrayList<Predicate>();

		CriteriaBuilder cb = this.getEntityManager().getCriteriaBuilder();

		CriteriaQuery<VersionableEquipment> cq  = cb.createQuery(VersionableEquipment.class);
		
		Root<VersionableEquipment> versinableEquipment = cq.from(VersionableEquipment.class);
		
		Join<VersionableEquipment,Equipment> joinEquipment = versinableEquipment.join(VersionableEquipment_.equipmentVersions,JoinType.LEFT);
		
		Predicate pred;
		
	    for (Map.Entry<String,Object> line : fields.entrySet()) {
	    
	    	if(line.getKey().toString() == "name"){
	    		pred = cb.like(cb.upper(joinEquipment.get(Equipment_.name)), "%"+line.getValue().toString().toUpperCase()+"%");
	    		predicateList.add(pred);    		

	    	}else if(line.getKey().toString() == "description"){
	    		pred = cb.like(cb.upper(joinEquipment.get(Equipment_.description)), "%"+line.getValue().toString().toUpperCase()+"%");
	    		predicateList.add(pred);    		
	    	
	    	}else if(line.getKey().toString() == "obs"){
	    		pred = cb.like(cb.upper(joinEquipment.get(Equipment_.obs)),	"%"+line.getValue().toString().toUpperCase()+"%");
	    		predicateList.add(pred);    		
	    	
	    	}else if( line.getKey().toString() == "begChangedAt" ){ 
	    		pred = cb.greaterThanOrEqualTo(joinEquipment.get(Equipment_.lastChange), this.begDayDateHour((Date)line.getValue()));
	    		predicateList.add(pred);    		
	    	
	    	}else if( line.getKey().toString() == "endChangedAt" ){ 
	    		pred = cb.lessThanOrEqualTo(joinEquipment.get(Equipment_.lastChange), this.endDayDateHour((Date)line.getValue()));
	    		predicateList.add(pred);    		
	    	
	    	}else if( line.getKey().toString() == "begCreatedAt" ){ 
	    		pred = cb.greaterThanOrEqualTo(joinEquipment.get(Equipment_.createdAt), this.begDayDateHour((Date)line.getValue()));
	    		predicateList.add(pred);    		
	    	
	    	}else if( line.getKey().toString() == "endCreatedAt" ){ 
	    		pred = cb.lessThanOrEqualTo(joinEquipment.get(Equipment_.createdAt), this.endDayDateHour((Date)line.getValue()));
	    		predicateList.add(pred);    		
	    	
	    	}else if( line.getKey().toString() == "librarys" ){
	    		Expression<Library> lib =  versinableEquipment.get(VersionableEquipment_.library);
    	
	    		pred = lib.in((List<Library>)line.getValue());
	    		predicateList.add(pred);    		

	    	}else if( line.getKey().toString() == "active"){ 
	    		pred = cb.equal(joinEquipment.get(Equipment_.active), (Boolean)line.getValue());
	    		predicateList.add(pred);    		
	       	}	    	
	    	
	    }
	    
	    Predicate[] predicates = new Predicate[predicateList.size()];
	    predicateList.toArray(predicates);
	    cq.where(predicates);
	 
	    
	    return super.getEntityManager().createQuery(cq).getResultList();		
	}

}
